{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "28a6d3d7",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "# 编码器-解码器架构"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "07aa3e88",
   "metadata": {},
   "source": [
    "- —个模型被分为两块:\n",
    "    - 编码器处理输入\n",
    "    - 解码器生成输出\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f7e1f404",
   "metadata": {},
   "source": [
    "<img src='encoder_decoder.png'>"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "40b942b9",
   "metadata": {},
   "source": [
    "\n",
    "编码器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "66b6f635",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:51:38.545732Z",
     "iopub.status.busy": "2022-07-31T02:51:38.545487Z",
     "iopub.status.idle": "2022-07-31T02:51:39.255500Z",
     "shell.execute_reply": "2022-07-31T02:51:39.254503Z"
    },
    "origin_pos": 2,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "from torch import nn\n",
    "\n",
    "\n",
    "class Encoder(nn.Module):\n",
    "    \"\"\"编码器-解码器架构的基本编码器接口\"\"\"\n",
    "    def __init__(self, **kwargs):\n",
    "        super(Encoder, self).__init__(**kwargs)\n",
    "\n",
    "    def forward(self, X, *args):\n",
    "        raise NotImplementedError"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0238eb62",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "解码器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b0b30606",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:51:39.259799Z",
     "iopub.status.busy": "2022-07-31T02:51:39.259343Z",
     "iopub.status.idle": "2022-07-31T02:51:39.265079Z",
     "shell.execute_reply": "2022-07-31T02:51:39.263985Z"
    },
    "origin_pos": 6,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "class Decoder(nn.Module):\n",
    "    \"\"\"编码器-解码器架构的基本解码器接口\"\"\"\n",
    "    def __init__(self, **kwargs):\n",
    "        super(Decoder, self).__init__(**kwargs)\n",
    "\n",
    "    def init_state(self, enc_outputs, *args):\n",
    "        raise NotImplementedError\n",
    "\n",
    "    def forward(self, X, state):\n",
    "        raise NotImplementedError "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "86293b0d",
   "metadata": {},
   "source": [
    "【raise NotImplementedErro】\n",
    "\n",
    "在面向对象编程中，可以在父类中先预留一个方法接口不实现，在其子类中实现。如果要求其子类一定要实现，不实现的时候会导致问题，那么采用raise的方式就很好。\n",
    "\n",
    "而此时产生的问题分类是NotImplementedError。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e40fddf2",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "合并编码器和解码器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5ae291c3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:51:39.268486Z",
     "iopub.status.busy": "2022-07-31T02:51:39.268263Z",
     "iopub.status.idle": "2022-07-31T02:51:39.274376Z",
     "shell.execute_reply": "2022-07-31T02:51:39.273349Z"
    },
    "origin_pos": 10,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "class EncoderDecoder(nn.Module):\n",
    "    \"\"\"编码器-解码器架构的基类\"\"\"\n",
    "    def __init__(self, encoder, decoder, **kwargs):\n",
    "        super(EncoderDecoder, self).__init__(**kwargs)\n",
    "        self.encoder = encoder\n",
    "        self.decoder = decoder\n",
    "\n",
    "    def forward(self, enc_X, dec_X, *args):\n",
    "        enc_outputs = self.encoder(enc_X, *args)\n",
    "        dec_state = self.decoder.init_state(enc_outputs, *args)\n",
    "        return self.decoder(dec_X, dec_state)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "language_info": {
   "name": "python"
  },
  "rise": {
   "autolaunch": true,
   "enable_chalkboard": true,
   "overlay": "<div class='my-top-right'><img height=80px src='http://d2l.ai/_static/logo-with-text.png'/></div><div class='my-top-left'></div>",
   "scroll": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
